﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Linq.Expressions;
using System.IO;

public partial class Manage_ListTeachers : System.Web.UI.Page
{
    protected PagedDataSource pds;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region 加载单位下拉列表框
            Departments.Items.Add(Toolkit.CurrentUserBelong);
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Department.xml");
            xds.XPath = "//Depart";

            Departments.DataSource = xds;
            Departments.DataTextField = "name";
            Departments.DataValueField = "name";
            Departments.DataBind();

            Departments.Items.Insert(0, new ListItem("请选择", ""));
            #endregion

            BindData();
        }
    }

    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aT in db.Teacher
                select aT;

        #region 检索单位列表框
        if (Departments.SelectedValue != "")
        {
            q = q.Where(aT => aT.Belong == Departments.SelectedValue);
        }
        #endregion

        #region 检索条件
        string search = SearchConditionHidden.Value.Trim();
        if (search != "")
        {
            Expression<Func<Teacher, bool>> lmbda = PredicateBuilder<Teacher>.CreatePredicate(
                new string[] { 
                    "Name", "Gender", "Unit", "Education",
                    "Courses", "Policital", "Duty","Profession",
                    "Rank","Nation","Memo"}, search, "aT");
            q = q.Where(lmbda);
        } 
        #endregion

        var p = q.OrderBy(aT => aT.Name).Select(aT => new
                {
                    ID = aT.ID,
                    Name = aT.Name,
                    Gender = aT.Gender,
                    Units = aT.Unit,
                    Education = aT.Education,
                    Course = aT.Courses.Split('|'),
                    Policital = aT.Policital,
                    Duty = aT.Duty,
                    Profession = aT.Profession,
                    Rank = aT.Rank,
                    Nation = aT.Name,
                    Memo = aT.Memo,
                    District = aT.District,
                    Belong = aT.Belong,
                    LoginName = aT.Managers.First().ID,
                    Verified = aT.Verfied,
                    Verifier = aT.Verifier.Name,
                    CanCheck = Toolkit.HasPower(Powers.SystemManager) ? true : ((aT.Verfied == null) && (Toolkit.HasPower(Powers.TeacherManager)) && (Toolkit.CurrentUserBelong == aT.Belong)) ? true : false
                });

        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = Toolkit.PageSize;
        pds.DataSource = p.ToList();


        #region 获取当前要显示的页
        int pageIndex = 0;
        pageIndex = Convert.ToInt32(CurrentPage.Value) - 1;
        if (pageIndex >= pds.PageCount)
        {
            pageIndex = pds.PageCount - 1;
        }
        if (pageIndex <= 0)
        {
            pageIndex = 0;
        }
        #endregion

        pds.CurrentPageIndex = pageIndex;

        RecordView.DataSource = pds;
        RecordView.DataBind();

        CurrentPage.Value = Convert.ToString(pageIndex + 1);
        SearchCondition.Text = SearchConditionHidden.Value.Trim();
    }
    protected void ChangePage(object sender, CommandEventArgs e)
    {
        string kind = Convert.ToString(e.CommandArgument);
        int p = Convert.ToInt32(CurrentPage.Value);

        if (kind == "F")
        {
            CurrentPage.Value = "1";
        }
        else if (kind == "P")
        {
            p--;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "N")
        {
            p++;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "L")
        {
            CurrentPage.Value = Convert.ToString(Int32.MaxValue);
        }
        else if (kind == "G")
        {
            TextBox gtp;
            foreach (Control c in RecordView.Controls)
            {
                gtp = c.FindControl("GotoPage") as TextBox;
                if (gtp != null)
                {
                    try
                    {
                        CurrentPage.Value = Convert.ToString(Convert.ToInt32(gtp.Text.Trim()));
                    }
                    catch
                    {
                        //CurrentPage.Value = "1";
                    }

                    break;
                }
            }
        }

        BindData();
    }
    protected void Delete_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();
        string[] ids = Request.Params["chkID"].Split(',');

        try
        {
            var q = from aT in db.Teacher
                    where ids.Contains(aT.ID)
                    select aT;

            foreach (Teacher teacher in q)
            {
                //删除登录信息
                db.Manager.DeleteAllOnSubmit(teacher.Managers);

                //删除Invite中的信息
                foreach (Invite inv in teacher.Invite)
                {
                    inv.TeacherID = null;
                }

                //删除Train中的信息
                foreach (Train train in teacher.Train)
                {
                    train.TeacherID = null;
                }

                //删除Answer中的信息
                foreach (Answer answer in teacher.Answer)
                {
                    answer.TeacherID = null;
                }

                #region 删除教师对应的照片
                string file = Toolkit.MappingPath(teacher.Photo);
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                #endregion
            }

            #region 删除教师信息
            db.Teacher.DeleteAllOnSubmit(q);
            #endregion

            db.SubmitChanges();

            MessageBox.Message = "教师信息已经成功删除。";
            MessageBox.Location = "./ListTeachers.aspx";
        }
        catch (Exception ee)
        {
            MessageBox.Message = ee.Message;
        }

        BindData();
    }
    protected void Search_Click(object sender, EventArgs e)
    {
        SearchConditionHidden.Value = SearchCondition.Text.Trim();
        BindData();
    }
    protected void Departments_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
}